package com.scm.controller;

/**
 * @author CHL
 * @ClassName: CategoryController
 * @Description: TODO
 * @date 2022/3/8 14:21
 * @Version 1.0
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.CustomerMapper;
import com.scm.entity.Customer;
import com.scm.entity.response.ResponseMessage;
import com.scm.util.DateUtil;
import com.scm.util.StringUtil;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/sell"})
public class CustomerController
{
    @Autowired
    private CustomerMapper customerMapper;
    private ResponseMessage rm = new ResponseMessage();

    public boolean checkCustomer(Customer customer)
    {
        if (StringUtil.isEmpty(customer))
        {
            this.rm.setMessage("用户信息不能为空");
            return false;
        }
        String customerCode = customer.getCustomerCode();
        if (!customerCode.matches("^[a-zA-Z0-9]{4,20}$"))
        {
            this.rm.setMessage("客户编码customerCode参数：要求4-20位的数字字母");
            return false;
        }
        String name = customer.getName();
        if (StringUtil.isEmpty(name))
        {
            this.rm.setMessage("name:不能为空");
            return false;
        }
        if (name.length() > 100)
        {
            this.rm.setMessage("name长度不超过100字符");
            return false;
        }
        String password = customer.getPassWord();
        if (StringUtil.isEmpty(password))
        {
            this.rm.setMessage("password不能为空");
            return false;
        }
        if ((password.length() > 20) || (password.length() < 4))
        {
            this.rm.setMessage("password长度为4~20位");
            return false;
        }
        String createDate = customer.getCreateDate();
        if (StringUtil.isEmpty(createDate))
        {
            this.rm.setMessage("注册时间createDate不能为空");
            return false;
        }
        if (!DateUtil.isValid(createDate))
        {
            this.rm.setMessage("日期createDate格式不正确");
            return false;
        }
        String tel = customer.getTel();
        if (StringUtil.isEmpty(tel))
        {
            this.rm.setMessage("电话tel不能为空");
            return false;
        }
        return true;
    }

    @RequestMapping({"/customer/add"})
    public ResponseMessage customerAdd(Customer customer)
    {
        boolean flag = checkCustomer(customer);
        if (!flag)
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        this.customerMapper.addCustomer(customer);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("添加供应商成功");
        return this.rm;
    }

    @RequestMapping({"/customer/delete"})
    public ResponseMessage delete(String customerCode)
    {
        if (customerCode == null)
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("缺少必要参数customerCode");
        }
        else
        {
            this.customerMapper.deleteCustomer(customerCode);
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("删除成功");
            this.rm.setData(this.customerMapper.select(null));
        }
        return this.rm;
    }

    @RequestMapping({"/customer/update"})
    public ResponseMessage update(Customer customer)
    {
        boolean flag = checkCustomer(customer);
        if (!flag)
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        int n = this.customerMapper.updateCustomer(customer);
        if (n == 0)
        {
            this.rm.setCode(Integer.valueOf(4));
            this.rm.setMessage("修改失败");
        }
        else
        {
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("修改成功");
        }
        return this.rm;
    }

    @RequestMapping({"/customer/show"})
    public PageInfo<Customer> select(Customer customer, Integer page)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        List<Customer> customers = this.customerMapper.select(customer);
        PageInfo<Customer> info = new PageInfo(customers);
        return info;
    }

    @RequestMapping({"/customer/all"})
    public List<Customer> selectAll()
    {
        List<Customer> customers = this.customerMapper.select(null);
        return customers;
    }
}
